package com.skeeey.aim.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class RS {
	private static RS rs = null;
	
	private Map<String, String> map = new HashMap<String, String>();
	
	private RS() {
		InputStream in = null;
		File file = new File(AIMUtil.getSystemConfig("execl.rs.path"));
		try {
			in = new FileInputStream(file);
			Workbook workbook = new HSSFWorkbook(in);
			Sheet sheet = workbook.getSheetAt(0);
			for(int ri= 0; ri < sheet.getLastRowNum() + 1; ri++) {
				Row row = sheet.getRow(ri);
				map.put(AIMUtil.getValueFrom(row.getCell(0)), AIMUtil.getValueFrom(row.getCell(1)));
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		}
			
	}
	
	public String getAutoKindBy(String autoCode) {
		String autoKind = null;
		if(autoCode != null) {
			autoKind = map.get(autoCode);
		}
		return autoKind;
	}
	
	
	public synchronized static RS getInst() {
		if(rs == null) {
			rs = new RS();
		}
		
		return rs;
	}
	
	public static void main(String[] args) {
		System.out.println(RS.getInst().getAutoKindBy("BR23C4"));
	}

}
